package cool.taomu.toolkit.cacheable

import com.google.inject.Inject
import java.util.concurrent.TimeUnit
import redis.clients.jedis.JedisPool

class RedisCacheService implements ICacheService {

	@Inject
	JedisPool pool;

	override put(String key, byte[] value) {
		var jedis = pool.borrowObject
		try {
			jedis.set(key.bytes, value)
			jedis.expire(key.bytes, TimeUnit.MINUTES.toSeconds(5))
		} finally {
			pool.returnObject(jedis)
		}
	}

	override get(String key) {
		var jedis = pool.borrowObject
		try {
			return jedis.get(key.bytes)
		} finally {
			pool.returnObject(jedis)
		}
	}

	override remove(String key) {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override clear() {
		throw new UnsupportedOperationException("TODO: auto-generated method stub")
	}

	override put(String key, byte[] value, long expire) {
		var jedis = pool.borrowObject
		try {
			jedis.set(key.bytes, value)
			jedis.expire(key.bytes, expire)
		} finally {
			pool.returnObject(jedis)
		}
	}

}
